package org.invigorare.auctions.widgets;

import java.util.*;

import org.invigorare.auctions.data.entities.Licensee;
import org.invigorare.auctions.data.entities.Licensee.LicenseType;

import android.content.Context;
import android.util.Log;
import android.view.*;
import android.widget.*;

public class LicenseeAdapter extends ArrayAdapter<String> {

	int viewResId;
	Context context;
	Licensee[] array;
	List<Licensee> list;
	boolean useArray; // false = useList;
	LicenseType licenseTypeToDisplay;
	
	public LicenseeAdapter(Context context, int textViewResourceId, LicenseType licenseTypeToDisplay) {
		super(context, textViewResourceId);
		this.context = context;
		this.viewResId = textViewResourceId;
		this.licenseTypeToDisplay = licenseTypeToDisplay;
	}

	/**
	 * @param context
	 * @param textViewResourceId
	 * @param objects		an array of Licensee's to be displayed
	 * @param stringKeys	an array of Strings to be filtered against (must be in same order as array)
	 * @param licenseTypeToDisplay chooses whether Seller or Buyer license number is displayed
	 */
	public LicenseeAdapter(Context context, int textViewResourceId, Licensee[] objects, String[] stringKeys, LicenseType licenseTypeToDisplay) {
		super(context, textViewResourceId, stringKeys); // pass the desired String keys to the superclass, to be used as filtering criterion
		this.context = context;
		this.viewResId = textViewResourceId;
		this.licenseTypeToDisplay = licenseTypeToDisplay;
		
		this.array = objects;
		useArray = true;
	}

	/**
	 * 
	 * @param context
	 * @param textViewResourceId
	 * @param objects		a List of Licensee's to be displayed
	 * @param stringKeys	a List of Strings to be filtered against (must be in same order as the List)
	 * @param licenseTypeToDisplay chooses whether Seller or Buyer license number is displayed
	 */
	public LicenseeAdapter(Context context, int textViewResourceId, List<Licensee> objects, List<String> stringKeys, LicenseType licenseTypeToDisplay) {
		super(context, textViewResourceId, stringKeys); // pass the desired String keys to the superclass, to be used as filtering criterion
		this.context = context;
		this.viewResId = textViewResourceId;
		this.licenseTypeToDisplay = licenseTypeToDisplay;
		
		this.list = objects;
		useArray = false;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View v = convertView;
        if (v == null) {
            LayoutInflater vi = LayoutInflater.from(context);
            v = vi.inflate(viewResId, null);
        }
        
        Licensee lic = (useArray) ? array[position] : list.get(position);
        
        if (lic != null) {
        	LicenseeDisplayHelper.setLicenseeToDisplay(context, v, lic, licenseTypeToDisplay);
        }
        return v;
	}

	
	@Override
	public View getDropDownView(int position, View convertView, ViewGroup parent) {
		return getView(position, convertView, parent);
	}
}
